import {Body, Controller, HttpStatus, Post, Res} from '@nestjs/common';
import {Response} from 'express';
import {UserListService} from '../../service/user.list.service';
import {CommonRedisService} from '../../service/common.redis.service';
import {CommonResponseDto} from '../../dto/common.response.dto';
import {ResCode, UserStatus} from '../../common/constants';
import AppConfig from 'config/app.config';
import {getLogStr} from "../../common/utils";
import {Logger} from '../../utils/log4js';
import {CommonRequestService} from "../../service/common.request.service";

const exception = (e) => {
    console.log(e);
    try {
        Logger.error(getLogStr(e))
    } catch(e){}
    return new CommonResponseDto(ResCode.ERROR, '服务异常', null);
}
@Controller('user-list')
export class UserListController {
    constructor(private readonly userListService: UserListService, private readonly commonRedisService: CommonRedisService, private readonly commonRequestService: CommonRequestService) {}

    @Post('/list')
    async getList(@Body() params: {status: number, pageIndex: number, pageSize: number}, @Res({ passthrough: true }) res: Response) {
        res.status(HttpStatus.OK);
        params.status = params.status || UserStatus.NORMAL
        params.pageIndex = params.pageIndex || 1
        params.pageSize = params.pageSize || 10

        try {
            const data = await this.userListService.getUserList({status: params.status, pageIndex: params.pageIndex, pageSize: params.pageSize})
            if (data && data.length === 2) {
                return new CommonResponseDto(ResCode.OK, 'success', {
                    list: data[0],
                    total: data[1]
                })
            } else {
                return new CommonResponseDto(ResCode.OK, 'success', null)
            }

        } catch(e){
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }

    @Post('/save')
    async saveUser(@Body() params: {id: string, name: string, sex: number, status: number }, @Res({ passthrough: true }) res: Response) {
        res.status(HttpStatus.OK);
        params.id = params.id || '';
        params.name = params.name || '';
        params.sex = params.sex || 1;
        params.status = params.status || UserStatus.NORMAL;
        if (![UserStatus.NORMAL, UserStatus.DELETED].includes(params.status)) {
            params.status = UserStatus.NORMAL
        }
        if (!params.name) {
            return new CommonResponseDto(ResCode.ERROR, '用户名必填', null)
        }
        try {
            let data
            if (params.id) {
                data = await this.userListService.editOne({
                    id: params.id,
                    name: params.name,
                    sex: params.sex,
                    status: params.status
                })
            } else {
                data = await this.userListService.createOne({
                    id: +new Date()+'',
                    name: params.name,
                    sex: params.sex,
                    status: params.status
                })
            }
            return new CommonResponseDto(ResCode.OK, 'success', data)
        } catch(e){
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }
}
